<template>
  <div class="home">
    <!-- 总布局容器 -->
    <el-container>
      <!-- 页面头部 -->
      <el-header>
        <!-- LOGO部分 -->
        <div>
          <img src="../assets/002.png" alt="">
          <span>电子商城管理后台</span>
        </div>
        <el-button type="success" @click="logout()" size="mini">退出按钮</el-button>
      </el-header>
      <!-- 包裹侧边栏和主页面的布局容器 -->
      <el-container>
        <!-- 侧边栏 -->
        <el-aside :width="isCollapse ? '64px' : '200px'">
          <!-- 收缩按钮 -->
          <div class="toggleBtn" @click="toggleCollapse()">
              <i :class="isCollapse ? 'el-icon-s-unfold' : 'el-icon-s-fold'"></i>
          </div>
          <!-- 左侧菜单区域 -->
          <el-menu  class="el-menu-vertical-demo" background-color="#1f334e" text-color="#fff"
            active-text-color="#ffd04b" :unique-opened="true" :collapse="isCollapse" :collapse-transition="false"
            :router="true" :default-active="activePath">
            <!-- 一级菜单 -->
            <el-submenu :index="item.id + ''" v-for="item in menulist" :key="item.id">
              <!-- 一级菜单的模板 -->
              <template slot="title">
                <!-- 菜单图标 -->
                <i :class="iconObj[item.id]"></i>
                <!-- 文本 -->
                <span>{{ item.name }}</span>
              </template>

              <!-- 一级菜单下的二级菜单 -->
              <el-menu-item :index="subitem.path + ''" v-for="subitem in item.child" :key="subitem.id"
                @click="saveMenuStatus(subitem.path)">
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span>{{ subitem.name }}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>
        <!-- 主页面 -->
        <el-main>
          <!-- 路由渲染安放区域 -->
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
// @ is an alias to /src
//import HelloWorld from "@/components/HelloWorld.vue";

export default {
  data() {
    return {
      menulist: [],  //菜单对象
      iconObj: {  //图标数组
        '1': 'el-icon-user-solid',
        '2': 'el-icon-s-flag',
        '3': 'el-icon-s-cooperation',
        '4': 'el-icon-s-order',
        '5': 'el-icon-s-data',
      },
      isCollapse: false,  //是否收缩
      activePath:""  //当前激活的路径
    };
  },
  name: "Home",
  components: {
    //HelloWorld
  },
  methods: {
    logout() {  //退出方法
      this.$confirm('您确定要退出吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        //1、清空令牌
        window.sessionStorage.clear();
        //2、跳转到登陆页面
        this.$router.push('/login');
      }).catch(() => {
      });
    },
    async getmenus() {
      const res = await this.$http.get('/api/menu')  //发请求
      console.log(res.data)
      if (!res.status == 'ok') return  //停止
      this.menulist = (res.data).data   //将数据赋值给菜单对象
    },
    toggleCollapse(){
      //点击切换让菜单展开或折叠
      this.isCollapse = !this.isCollapse
    },
    saveMenuStatus(activePath){  //保存当前的二级菜单展开列表
      window.sessionStorage.setItem('activePath',activePath)  //在系统里保存展开的列表
      this.activePath = activePath
    }
  },
  created() {
    this.getmenus()
    this.activePath = window.sessionStorage.getItem('activePath')  //系统启动的时候，拿到展开过的列表，并复原
  }

};
</script>

<style scoped>
.toggleBtn {
  line-height: 24px;
  color: #fff;
  text-align: left;
  font-size: 16px;
  padding-left: 10px;
  cursor: pointer;
}

.el-container {
  height: 100%;
}

.el-header {
  background-color: #1f334e;
  color: white;
  height: 80px !important;
  display: flex;
  /* 两端对齐 */
  justify-content: space-between;
  align-items: center;
}

.el-header div {
  display: flex;
  align-items: center;
}

.el-header img {
  width: 60px;
  height: 60px;
  padding-right: 6px;
}

.el-aside {
  background-color: #1f334e;
  line-height: 400px;
  text-align: left;
}

.iconfont {
  margin-right: 10px;
}

.el-main {
  background-color: #e9eef3;
  /* line-height: 400px; */
}

.el-menu {
  border-right: none;
}
</style>